package br.com.amadeus.android.util;

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

public class SchedulerSyncronize {
	
	public static int parameterToBroadcast;
		
	public static PendingIntent startService(int minute, Context ctx, AlarmManager alarme, String serviceName){
		
		final Intent service = new Intent(serviceName);
		PendingIntent p = PendingIntent.getService(ctx, 0, service, 0);

		//Iniciar em
		final int minutos = minute;

		// Repetir a cada 1 * 60 * 1000 = 1 minuto
		final int tempoRepetir =  minute * 60 * 1000;

		// Para executar o alarme depois de 1 segundo a partir de agora
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(System.currentTimeMillis());
		c.add(Calendar.MINUTE, minutos);
		
		long time = c.getTimeInMillis();
		alarme.setRepeating(AlarmManager.RTC_WAKEUP, time, tempoRepetir, p);
		
		return p;
	}
	
	public static void sendBroadcast(int second, Context ctx, AlarmManager alarme, String broadcastName, int id){
		
		// Intent para disparar o broadcast
		Intent it = new Intent(broadcastName);
		parameterToBroadcast = id;
		PendingIntent p = PendingIntent.getBroadcast(ctx, 0, it, 0);
	
		// Para executar o alarme depois de x segundos a partir de agora
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(System.currentTimeMillis());
		c.add(Calendar.SECOND, second);
	
		long time = c.getTimeInMillis();
		alarme.set(AlarmManager.RTC_WAKEUP, time, p);
	}

}
